﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Test
{
    public class HistoryDataRetriever
    {
        #region Trading Data
        public void GetTradingDataFromCafef()
        {
            try
            {
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://cafef.vn/Lich-su-giao-dich-Symbol-PIT/Trang-1-0-tab-2.chn");
                using (HttpWebResponse siteResponse = (HttpWebResponse)myWebRequest.GetResponse())
                {
                    using (Stream streamResponse = siteResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(streamResponse, Encoding.UTF8))
                        {
                            var stockData = reader.ReadToEnd();
                            stockData = stockData.Replace(Environment.NewLine, String.Empty)
                                                 .Replace("&nbsp;", String.Empty);

                            Regex regex = new Regex(@"\<tr id="".+rptData.+Tr((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            //Regex regex = new Regex(@"\<trid=""ctl18_rptData((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            var stockInfoList = regex.Matches(stockData);

                            if (stockInfoList.Count > 0)
                            {
                                ParseTradingData(stockInfoList[stockInfoList.Count - 1].Value);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void ParseTradingData(string tradingData)
        {
            tradingData = RefineTradingData(tradingData);
            XDocument document = XDocument.Parse(tradingData);
            foreach (var transactionItem in document.Root.Elements("tr"))
            {
                var valueList = transactionItem.Elements("td").ToList();
                ShowValue(valueList, 0, "Date");
                ShowValue(valueList, 1, "Du mua");
                ShowValue(valueList, 2, "Du ban");
                ShowValue(valueList, 3, "Thay doi");
                ShowValue(valueList, 4, "So lenh mua");
                ShowValue(valueList, 5, "Khoi luong dat mua");
                ShowValue(valueList, 6, "KLTB 1 lenh dat mua");
                ShowValue(valueList, 7, "So lenh ban");
                ShowValue(valueList, 8, "Khoi luong dat ban");
                ShowValue(valueList, 9, "KLTB 1 lenh dat ban");
                ShowValue(valueList, 10, "Chenh lech mua/ban");
            }
        }

        private string RefineTradingData(string tradingData)
        {
            Regex regex = new Regex(@"\<span((?!(\<\/span\>)).)*\<\/span\>");
            tradingData = regex.Replace(tradingData, String.Empty);
            regex = new Regex(@"\<img((?!(\>)).)*\>");
            tradingData = regex.Replace(tradingData, String.Empty);

            tradingData = String.Format("<TradingData>{0}</TradingData>", tradingData);

            return tradingData;
        } 
        #endregion

        #region Price Data
        public void GetPriceDataFromCafef()
        {
            try
            {
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://cafef.vn/Lich-su-giao-dich-Symbol-TRA/Trang-1-0-tab-1.chn");
                using (HttpWebResponse siteResponse = (HttpWebResponse)myWebRequest.GetResponse())
                {
                    using (Stream streamResponse = siteResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(streamResponse, Encoding.UTF8))
                        {
                            var stockData = reader.ReadToEnd();
                            stockData = stockData.Replace(Environment.NewLine, String.Empty)
                                                 .Replace("&nbsp;", String.Empty);

                            Regex regex = new Regex(@"\<tr id="".+rptData((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            //Regex regex = new Regex(@"\<trid=""ctl18_rptData((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            var stockInfoList = regex.Matches(stockData);

                            if (stockInfoList.Count > 0)
                            {
                                ParsePriceData(stockInfoList[stockInfoList.Count - 1].Value);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void ParsePriceData(string priceData)
        {
            priceData = RefinePriceData(priceData);
            XDocument document = XDocument.Parse(priceData);
            foreach (var transactionItem in document.Root.Elements("tr"))
            {
                var valueList = transactionItem.Elements("td").ToList();
                ShowValue(valueList, 0, "Date");
                ShowValue(valueList, 1, "Gia dong cua");
                ShowValue(valueList, 3, "Thay doi");
                ShowValue(valueList, 5, "Gia tham chieu");
                ShowValue(valueList, 6, "Gia mo cua");
                ShowValue(valueList, 7, "Gia cao nhat");
                ShowValue(valueList, 8, "Gia thap nhat");
                ShowValue(valueList, 9, "Khoi luong giao dich khop lenh");
                ShowValue(valueList, 10, "Gia tri giao dich khop lenh");
                ShowValue(valueList, 11, "Khoi luong giao dich thoa thuan");
                ShowValue(valueList, 12, "Gia tri giao dich thoa thuan");
            }
        }

        private string RefinePriceData(string priceData)
        {
            Regex regex = new Regex(@"\<span((?!(\<\/span\>)).)*\>");
            priceData = regex.Replace(priceData, String.Empty);
            regex = new System.Text.RegularExpressions.Regex(@"\<\/span\>");
            priceData = regex.Replace(priceData, String.Empty);
            regex = new Regex(@"\<img((?!(\>)).)*\>");
            priceData = regex.Replace(priceData, String.Empty);
            regex = new Regex(@"\([-\.\d]+\s%\)");
            priceData = regex.Replace(priceData, String.Empty);

            priceData = String.Format("<PriceData>{0}</PriceData>", priceData);

            return priceData;
        }  
        #endregion

        #region Foreign Data
        public void GetForeignDataFromCafef()
        {
            try
            {
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://cafef.vn/Lich-su-giao-dich-Symbol-TRA/Trang-1-0-tab-3.chn");
                using (HttpWebResponse siteResponse = (HttpWebResponse)myWebRequest.GetResponse())
                {
                    using (Stream streamResponse = siteResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(streamResponse, Encoding.UTF8))
                        {
                            var stockData = reader.ReadToEnd();
                            stockData = stockData.Replace(Environment.NewLine, String.Empty)
                                                 .Replace("&nbsp;", String.Empty);

                            Regex regex = new Regex(@"\<tr id="".+rptData.+TR((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            //Regex regex = new Regex(@"\<trid=""ctl18_rptData((?!(\<\/tr\>)).)*(\<\/tr\>)"); //.*"".+\<\/tr\>");
                            var stockInfoList = regex.Matches(stockData);

                            if (stockInfoList.Count > 0)
                            {
                                ParseForeignData(stockInfoList[stockInfoList.Count - 1].Value);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void ParseForeignData(string priceData)
        {
            priceData = RefineForeignData(priceData);
            XDocument document = XDocument.Parse(priceData);
            foreach (var transactionItem in document.Root.Elements("tr"))
            {
                var valueList = transactionItem.Elements("td").ToList();
                ShowValue(valueList, 0, "Date");
                ShowValue(valueList, 1, "KL Giao dich rong");
                ShowValue(valueList, 2, "Gia tri giao dich rong");
                ShowValue(valueList, 3, "Thay doi");
                ShowValue(valueList, 4, "Khoi luong mua");
                ShowValue(valueList, 5, "Gia tri mua");
                ShowValue(valueList, 6, "Khoi luong ban");
                ShowValue(valueList, 7, "Gia tri ban");
                ShowValue(valueList, 8, "Room con lai");
                ShowValue(valueList, 9, "Dang so huu");
            }
        }

        private string RefineForeignData(string foreignData)
        {
            Regex regex = new Regex(@"\<span((?!(\<\/span\>)).)*\<\/span\>");
            foreignData = regex.Replace(foreignData, String.Empty);
            regex = new Regex(@"\<img((?!(\>)).)*\>");
            foreignData = regex.Replace(foreignData, String.Empty);

            foreignData = String.Format("<ForeignData>{0}</ForeignData>", foreignData);

            return foreignData;
        }
        #endregion

        private void ShowValue(IList<XElement> valueList, int index, string title)
        {
            Console.WriteLine("{0}: {1}", title, valueList[index].Value.Trim());
        }
    }
}
